package com.gzx.qc.modular.voice.check.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzx.qc.core.config.QaKeywordInitializationConfig;
import com.gzx.qc.modular.voice.check.entity.GzxVoiceCheck;
import com.gzx.qc.modular.voice.check.entity.GzxVoiceCheckDetail;
import com.gzx.qc.modular.voice.check.entity.bo.GzxVoiceCheckEditBo;
import com.gzx.qc.modular.voice.check.entity.bo.GzxVoiceCheckQueryBo;
import com.gzx.qc.modular.voice.check.entity.vo.GzxVoiceCheckVo;
import com.gzx.qc.modular.voice.check.mapper.GzxVoiceCheckDetailMapper;
import com.gzx.qc.modular.voice.check.mapper.GzxVoiceCheckMapper;
import com.gzx.qc.modular.voice.check.service.GzxVoiceCheckService;
import com.gzx.qc.modular.voice.text.entity.GzxHtVoiceText;
import com.gzx.qc.modular.voice.text.mapper.GzxHtVoiceTextMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.util.CommonExcelUtil;
import vip.xiaonuo.common.util.MapstructUtils;

import java.util.List;

/**
 * @author zjt
 * @description
 * @date 2025/1/24 15:50
 */
@Service
@RequiredArgsConstructor
public class GzxVoiceCheckServiceImpl implements GzxVoiceCheckService {

    private final GzxVoiceCheckMapper voiceCheckMapper;

    private final GzxVoiceCheckDetailMapper voiceCheckDetailMapper;

    private final QaKeywordInitializationConfig qaKeywordConfig;

    private final GzxHtVoiceTextMapper htVoiceTextMapper;

    @Override
    public List<GzxVoiceCheck> selectList(LambdaQueryWrapper<GzxVoiceCheck> wrapper) {
        return voiceCheckMapper.selectList(wrapper);
    }

    //批量获取通话文本
    @DS("qc")
    public List<GzxHtVoiceText> getTextByCallIds(List<String> callIds) {
        return htVoiceTextMapper.selectList(Wrappers.<GzxHtVoiceText>lambdaQuery()
                .in(GzxHtVoiceText::getCallId, callIds)
                .orderByAsc(GzxHtVoiceText::getChatTime));

    }

    /**
     * 分页查询质检记录
     */
    @Override
    @DS("qc")
    public Page<GzxVoiceCheckVo> pageList(GzxVoiceCheckQueryBo voiceCheckQueryBo) {
        return voiceCheckMapper.selectVoPage(new Page<>(voiceCheckQueryBo.getCurrent(), voiceCheckQueryBo.getSize()), Wrappers.<GzxVoiceCheck>lambdaQuery()
                .eq(StringUtils.isNotEmpty(voiceCheckQueryBo.getCallId()), GzxVoiceCheck::getCallId, voiceCheckQueryBo.getCallId())
                .eq(StringUtils.isNotEmpty(voiceCheckQueryBo.getLiaisonPhone()), GzxVoiceCheck::getLiaisonPhone, voiceCheckQueryBo.getLiaisonPhone())
                .eq(StringUtils.isNotEmpty(voiceCheckQueryBo.getUserName()), GzxVoiceCheck::getUserName, voiceCheckQueryBo.getUserName())
                .eq(voiceCheckQueryBo.getDeptId() != null, GzxVoiceCheck::getDeptId, voiceCheckQueryBo.getDeptId())
                .like(StringUtils.isNotEmpty(voiceCheckQueryBo.getKeyword()), GzxVoiceCheck::getKeyword, voiceCheckQueryBo.getKeyword())
                .in(CollectionUtils.isNotEmpty(voiceCheckQueryBo.getQualityApproveStatus()), GzxVoiceCheck::getQualityApproveStatus, voiceCheckQueryBo.getQualityApproveStatus())
                .in(CollectionUtils.isNotEmpty(voiceCheckQueryBo.getSupervisorApproveStatus()), GzxVoiceCheck::getSupervisorApproveStatus, voiceCheckQueryBo.getSupervisorApproveStatus())
                //分总审核结果
                .in(CollectionUtils.isNotEmpty(voiceCheckQueryBo.getMasterApproveStatus()), GzxVoiceCheck::getMasterApproveStatus, voiceCheckQueryBo.getMasterApproveStatus())
                .in(CollectionUtils.isNotEmpty(voiceCheckQueryBo.getAiApproveStatus()), GzxVoiceCheck::getAiApproveStatus, voiceCheckQueryBo.getAiApproveStatus())
                .between(StringUtils.isNotBlank(voiceCheckQueryBo.getBeginTime()), GzxVoiceCheck::getCreateTime, voiceCheckQueryBo.getBeginTime() + " 00:00:00", voiceCheckQueryBo.getEndTime() + " 23:59:59")
                .orderByDesc(GzxVoiceCheck::getId));
    }

    /**
     * 导出质检
     */
    @Override
    @DS("qc")
    public void exportVoiceCheck(GzxVoiceCheckQueryBo voiceCheckQueryBo, HttpServletResponse response) {
        //导出不限制条数
        voiceCheckQueryBo.setCurrent(1);
        voiceCheckQueryBo.setSize(Integer.MAX_VALUE);
        //调用分页查询
        Page<GzxVoiceCheckVo> list = pageList(voiceCheckQueryBo);
        List<GzxVoiceCheckVo> records = list.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            //id转name等在setVoiceCheck中配置
            setVoiceCheck(records);
            CommonExcelUtil.exportExcel(records.getFirst().getCallId(), records, GzxVoiceCheckVo.class, response);
        } else {
            throw new CommonException("无可导出的数据");
        }
    }

    //封装导出数据
    private void setVoiceCheck(List<GzxVoiceCheckVo> records) {
        //审核结果
        for (GzxVoiceCheckVo record : records) {
            if (ObjectUtils.isEmpty(record.getQualityApproveRemark())) {
                record.setQualityApproveRemark("暂未进行审核");
            }
            if (ObjectUtils.isEmpty(record.getSupervisorApproveRemark())) {
                record.setSupervisorApproveRemark("暂未进行审核");
            }
            if (ObjectUtils.isEmpty(record.getMasterApproveRemark())) {
                record.setMasterApproveRemark("暂未进行审核");
            }
            if (ObjectUtils.isEmpty(record.getAiReasonContent())) {
                record.setAiReasonContent("暂未进行审核");
            }
        }
    }

    /**
     * 批量新增质检主表
     *
     * @param voiceCheckList
     */
    @Override
    @DS("qc")
    public void insertIgnore(List<GzxVoiceCheck> voiceCheckList) {
        voiceCheckMapper.batchInsertIgnore(voiceCheckList);
    }

    /**
     * 批量新增
     *
     * @param voiceCheckDetailList
     */
    @Override
    @DS("qc")
    public void insertBatchDetail(List<GzxVoiceCheckDetail> voiceCheckDetailList) {
        voiceCheckDetailMapper.insertBatch(voiceCheckDetailList);
    }

    /**
     * 修改违规状态
     *
     * @param voiceCheckEditBo
     */
    @Override
    @DS("qc")
    public void modifyViolationStatus(GzxVoiceCheckEditBo voiceCheckEditBo) {
        if ("supervisor".equals(voiceCheckEditBo.getType())) {
            if (voiceCheckEditBo.getSupervisorApproveStatus() == 1 || voiceCheckEditBo.getSupervisorApproveStatus() == 3) {
                //添加到黑名单
            }
            if (voiceCheckEditBo.getSupervisorApproveStatus() == 2 || voiceCheckEditBo.getSupervisorApproveStatus() == 3) {
                //添加到员工处罚
            }
        }
        //分总审批，如果传了 master_approve_status和 master_approve_remark就代表是分总审批
        voiceCheckMapper.updateById(MapstructUtils.convert(voiceCheckEditBo, GzxVoiceCheck.class));
    }

    @DS("qc")
    @Override
    public List<GzxVoiceCheck> getVoiceCheckList() {
        LambdaQueryWrapper<GzxVoiceCheck> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GzxVoiceCheck::getStatus, 0);
        wrapper.last("limit 1000");
        return voiceCheckMapper.selectList(wrapper);
    }

    /**
     * 更新Ai判断的状态
     *
     * @param gzxVoiceCheck
     */
    @Override
    @DS("qc")
    public void updateById(GzxVoiceCheck gzxVoiceCheck) {
        voiceCheckMapper.updateById(gzxVoiceCheck);
    }

    @Override
    @DS("qc")
    public List<GzxVoiceCheckDetail> detailKeywordByCallId(String callId) {
        LambdaQueryWrapper<GzxVoiceCheckDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(GzxVoiceCheckDetail::getKeyword, GzxVoiceCheckDetail::getLevel);
        wrapper.eq(GzxVoiceCheckDetail::getCallId, callId);
        return voiceCheckDetailMapper.selectList(wrapper);
    }

    @Override
    @DS("qc")
    public List<GzxVoiceCheckDetail> getGzxVoiceCheckDetailBywarpper(LambdaQueryWrapper<GzxVoiceCheckDetail> wrapper) {
        return voiceCheckDetailMapper.selectList(wrapper);
    }
}
